package  
{
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxG;
	import org.flixel.FlxU;
	import org.flixel.*;
	import org.flixel.system.input.Keyboard;
	import player.PlayerController;
	import player.Salazar;
	import pessoas.*;
	
	/**
	 * ...
	 * @author João Brant
	 */
	public class MainState extends FlxState 
	{
		[Embed(source = "../assets/som/main_music_Drink-Up-Westley.mp3")]
		public var mainMusic:Class;
		
		[Embed(source = "../assets/som/coin_short.mp3")]
		public var coinSound:Class;
		
		[Embed(source = "../assets/som/coin_long.mp3")]
		public var coinSound2:Class;
		
		public var paused:Boolean;
		public var pauseScreen:Pause;
		
		public var pessoasGroup:FlxGroup = new FlxGroup();
		
		public var salazar:Salazar;
		public var playerController:PlayerController;
		
		public var entityFactory:EntityFactory;
		
		public var keyboard:Keyboard = new Keyboard();
		
		public var linhaPessoasGroup:FlxGroup = new FlxGroup();
		public var linhaSalazarGroup:FlxGroup = new FlxGroup();
		
		public var moneyGroup:FlxGroup = new FlxGroup();
		
		private var moneyAmount:Number = 0;
		public function get MoneyAmount():Number
		{
			return moneyAmount;
		}
		public function set MoneyAmount(valor:Number):void
		{
			var delta:Number = valor - moneyAmount;
			
			moneyAmount += delta * (1 + (temImposto1 ? 0.5 : 0) + (temImposto2 ? 0.5 : 0) + (temImposto3 ? 1 : 0));
		}
		
		//Popularidade
		private var _popularity:Number = 100;
		public function get popularity():Number
		{
			return _popularity;
		}
		public function set popularity(valor:Number):void
		{
			_popularity = valor;
			if (_popularity > 100) _popularity = 100;
			if (_popularity < 0) _popularity = 0;
		}
		
		//Tempo
		public var timeLeft:Number;
		public var timeLeftBefore:Number
		
		public var speedUp:Boolean = false;
		
		//HUD
		public var hud:HUD = null;
		
		public static var levelVelocity:Number = 2;
		
		public static var street:Rua;
		public static var casas:Casas;
		
		public var screenAdjust:FlxPoint = new FlxPoint(0, 0);
		public var screenVelocity:int = 20;
		public var screenStop:Boolean = false;
		
		// linhas
		public var linha1:FlxSprite;
		public var linha2:FlxSprite;
		public var linha3:FlxSprite;
		public var linha4:FlxSprite;
		public var linha5:FlxSprite;
		
		// leis
		public var temImposto1:Boolean;
		public var temImposto2:Boolean;
		public var temImposto3:Boolean;
		public var temPropina1:Boolean;
		public var temPropina2:Boolean;
		public var temPropina3:Boolean;
		public var temCensura1:Boolean;
		public var temCensura2:Boolean;
		public var temCensura3:Boolean;
		
		// equips
		public var equipouSaidaPrisao:Boolean;
		public var usouSaidaPrisao:Boolean;
		
		public var equipouAcessorImprensa:Boolean;
		public var usouAcessorImprensa:Boolean;
		
		private var lives:int = 1;
		public function get Lives():int
		{
			return lives;
		}
			
		public function set Lives(value:int):void
		{
			lives = value;
			
			if (lives <= 0)
			{
				FlxG.resetState();
			}
		}
		
		public function MainState() 
		{
			entityFactory = new EntityFactory(this);
			super();
		}
		
		override public function create():void
		{
			pauseScreen = new Pause(this);
			
			hud = new HUD(this);
			timeLeft = 60;
			
			street = new Rua();
			casas = new Casas();
			add(street);
			add(casas);
			FlxG.worldBounds = new FlxRect( -Main.windowWidth, -600, Main.windowWidth * 3, 1800);
			
			getData();
			
			salazar = new Salazar(100, 210, this);
			
			linha1 = new FlxSprite( -200, 170);
			linha2 = new FlxSprite( -200, 1*Main.windowHeight/2);
			linha3 = new FlxSprite( -200, Main.windowHeight);
			linha4 = new FlxSprite(  30, 0);
			linha5 = new FlxSprite( Main.windowWidth-30, 0);
			
			linha1.solid = true; linha1.immovable = true;
			//linha2.solid = true; linha2.immovable = true;
			linha3.solid = true; linha3.immovable = true;
			linha4.solid = true; linha4.immovable = true;
			linha5.solid = true; linha5.immovable = true;
			
			linha1.makeGraphic(Main.windowWidth*3/2, 1, 0x00000000);
			//linha2.makeGraphic(Main.windowWidth*3/2, 1, 0xffff0000);
			linha3.makeGraphic(Main.windowWidth*3/2, 1, 0x00000000);
			linha4.makeGraphic(1, Main.windowHeight, 0x00000000);
			linha5.makeGraphic(1, Main.windowHeight, 0x00000000);
			
			linhaPessoasGroup.add(linha1);
			//linhaPessoasGroup.add(linha2);
			linhaPessoasGroup.add(linha3);
			linhaSalazarGroup.add(linha1);
			linhaSalazarGroup.add(linha3);
			linhaSalazarGroup.add(linha4);
			linhaSalazarGroup.add(linha5);
			
			street.y = linha2.y + linha2.height;
			salazar.y = linha2.y + 10;
			
			casas.y = -80;
			
			add(linhaPessoasGroup);
			add(linhaSalazarGroup);
			
			pessoasGroup.add(salazar);
			
			add(pessoasGroup);
			
			//add(scoreText);
			
			add(moneyGroup);
			
			playerController = new PlayerController(this, salazar);
			
			FlxG.playMusic(mainMusic, 1);
		}
		
		override public function update():void
		{
			if (paused)
			{
				pauseScreen.preUpdate();
				pauseScreen.update();
				pauseScreen.postUpdate();
				return;
			}
			else
			{
				pauseScreen.update();
			}
			
			super.update();
			
			if (timeLeft > 0)
			{
				timeLeft -= Main.delta;
			}
			else
			{
				FlxG.switchState(new ScoreScreen());
				PlayerData.dinheiro_lastSprint = moneyAmount;
				PlayerData.shopUnlocked = true;
			}
			
			var delta:int;
			if (!screenStop)
			{
				delta = Math.floor(screenAdjust.x + screenVelocity * Main.delta) - Math.floor(screenAdjust.x);
				screenAdjust.x += screenVelocity * Main.delta;
			}
			else
			{
				delta = 0;
			}
			
			if (new int(timeLeft) % 10 == 0)
			{
				if (!speedUp)
					screenVelocity += 2;
					
				speedUp = true;
			}
			else
			{
				speedUp = false;
			}
			
			//scoreText.x += delta;
			
			FlxG.worldBounds = new FlxRect(screenAdjust.x - Main.windowWidth, screenAdjust.y, Main.windowWidth * 3, Main.windowWidth * 3);

			FlxG.camera.scroll.x += delta;
			linha1.x += delta;
			linha2.x += delta;
			linha3.x += delta;
			linha4.x += delta;
			linha5.x += delta;
			
			pessoasGroup.sort("y", ASCENDING);
			
			entityFactory.update();
			
			FlxG.overlap(salazar, linhaSalazarGroup, collisionSalazar);
			FlxG.overlap(pessoasGroup, linhaPessoasGroup, bouncePessoa);
			FlxG.overlap(salazar, pessoasGroup, collisionPessoas);
			FlxG.overlap(salazar, moneyGroup, collisionMoney);
			
			playerController.update();
			hud.update();
			
			if (FlxG.keys.ESCAPE) pause();
		}
		
		override public function draw():void
		{
			super.draw();
			hud.draw();
			if (paused) pauseScreen.draw();
		}
		
		public function collisionSalazar(salazar:Salazar, linha:FlxSprite):void
		{
			if (FlxObject.separate(salazar, linha))
			{
				salazar.wallPushed = true;
			}
		}
		
		public function collisionPessoas(salazar:Salazar, pessoa:FlxSprite):void
		{
			if (pessoa is Policial)
			{
				var policial:Policial = pessoa as Policial;
				
				if (policial.ativo)
				{
					policial.ativo = false;
					playerController.prison();
				}
			}
			else if (pessoa is Midia)
			{
				var midia:Midia = pessoa as Midia;
				
				if (midia.ativo)
				{
					midia.ativo = false;
					playerController.slow(midia);
				}
			}
		}
		
		public function collisionMoney(salazar:Salazar, money:Money):void
		{
			if (Math.random() > 0.01)
			{
				FlxG.play(coinSound,0.3);
			}
			else
			{
				FlxG.play(coinSound2,0.4);
			}
			money.kill();
			moneyGroup.remove(money);
		}
		
		public function bouncePessoa(pessoa:FlxSprite, linha:FlxSprite):void
		{
			if (pessoa is Pessoa)
			{
				var p:Pessoa = pessoa as Pessoa;
				p.velocity.y *= -1;
				p.y += pessoa.velocity.y * Main.delta;
			}
		}
		
		public function getData():void
		{
			// Pegar o estado de uma classe que guarda se comprou as leis
			//leiImposto1.comprado = playerData.hasLeiImposto1;
			temImposto1 = PlayerData.hasImposto1;
			temImposto2 = PlayerData.hasImposto2;
			temImposto3 = PlayerData.hasImposto3;
			
			temPropina1 = PlayerData.hasPropina1;
			temPropina2 = PlayerData.hasPropina2;
			temPropina3 = PlayerData.hasPropina3;
			
			temCensura1 = PlayerData.hasCensura1;
			temCensura2 = PlayerData.hasCensura2;
			temCensura3 = PlayerData.hasCensura3;
			
			equipouSaidaPrisao = PlayerData.equipedSaidaPrisao;
			usouSaidaPrisao = false;
			
			equipouAcessorImprensa = PlayerData.equipedAcessorImprensa;
			usouAcessorImprensa = false;
		}
		
		public function stopScreen():void
		{
			timeLeftBefore = timeLeft;
			screenStop = true;
		}
		public function resumeScreen():void
		{
			screenStop = false;
		}
		
		public function pause():void
		{
			paused = true;
		}
		
		public function unpause():void
		{
			paused = false;
		}
	}
}